<template>
  <div>
    <el-button type="danger" @click="openDialog">添加</el-button>
    <!-- 全部打开：default-expand-all -->
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="firstcatename" label="一级分类"></el-table-column>
      <el-table-column prop="secondcatename" label="二级分类"></el-table-column>
      <el-table-column prop="goodsname" label="商品名称"></el-table-column>
      <el-table-column prop="img" label="商品图片">
        <template slot-scope="scope">
          <img v-if="scope.row.img" width="150px" :src="'http://localhost:3000'+scope.row.img" />
        </template>
      </el-table-column>
      <el-table-column prop="price" label="销售价格"></el-table-column>
      <el-table-column prop="market_price" label="市场价格"></el-table-column>
      <el-table-column prop="isnew" label="是否新品">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isnew"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="isNewChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="ishot" label="是否热卖">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.ishot"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="isHotChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==1" type="success" size="medium">已启用</el-tag>
          <el-tag v-else type="danger" size="medium">已禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            size="mini"
            @click="edit(scope.row.id)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="del(scope.row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="pageSize"
      layout="prev, pager, next, jumper,total"
      :total="total"
    ></el-pagination>
    <el-dialog :title="popType+'商品'" :visible.sync="dialogVisible" width="45%">
      <goods-info :transData="transData" :popType="popType"></goods-info>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialogVisible = false">取 消</el-button>
        <el-button type="danger" size="medium" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import goodsInfo from "./info";
import {
  getGoodsList,
  getGoodsById,
  getGoodsNumber,
  getCateById,
  getSecondCateList
} from "../../request";
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      popType: "新增",
      transData: {},
      currentPage: 1,
      pageSize: 3,
      total: 0
    };
  },
  components: {
    goodsInfo
  },
  created() {
    // 获取商品总数
    this.getGoodsNum();
  },
  methods: {
    async getGoodsNum() {
      let res = await getGoodsNumber();
      console.log(res);
      
      this.total = res[0].total;
      this.getData();
    },
    async getData() {
      let res = await getGoodsList({
        size: this.pageSize,
        page: this.currentPage
      });
      res.forEach(item => {
        item.isnew = item.isnew == 1 ? true : false;
        item.ishot = item.ishot == 1 ? true : false;
      });
      this.tableData = res;
    },
    handleCurrentChange(val) {
      this.getData();
    },
    openDialog() {
      this.popType = "新增";
      this.transData = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: [],
        isnew: 1,
        ishot: 1,
        status: 1
      };
      this.dialogVisible = true;
    },
    async edit(id) {
      this.popType = "修改";
      let res = await getGoodsById(id);
      res.isnew = res.isnew == 1 ? 1 : 2;
      res.ishot = res.ishot == 1 ? 1 : 2;
      res.specsattr = res.specsattr.split(",");
      this.transData = res;
      this.transData.id = id;
      this.dialogVisible = true;
    },
    del(id) {
      this.$confirm("确认删除吗,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type:"warning",
        confirmButtonClass:"el-button--danger"
      })
        .then(res => {
          this.doDel(id);
        })
        .catch(_ => {});
    },
    doDel(id) {
      this.$http.post('/goodsdelete', {id}).then(res => {
        this.$router.go(0);
      });
    },
    async isNewChange(data) {
      let isnew = data.isnew ? 1 : 0;
      // 声明一个formData类型的数据
      let formData = new FormData();
      formData.append("id", data.id);
      formData.append("isnew", isnew);
      this.$http
        .post("/goodsedit", formData)
        .then(res => {
          // this.$router.go(0);
        })
        .catch(err => {
          console.log(err);
        });
    },
    async isHotChange(data){
      let ishot = data.ishot ? 1 : 0;
      // 声明一个formData类型的数据
      let formData = new FormData();
      formData.append("id", data.id);
      formData.append("ishot", ishot);
      this.$http
        .post("/goodsedit", formData)
        .then(res => {
          // this.$router.go(0);
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>
<style lang="" >
.el-button {
  margin-bottom: 10px;
}
.el-pagination__total {
  float: right;
}
img {
  width: 100px;
}
</style>